//
// Created by LH on 2017/9/3.
//
#include <iostream>
#include <algorithm>
using namespace std;
int e[10][10];
int n, m;


int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (i == j)
                e[i][j] = 0;
            else
            {
                e[i][j] = 1e9;
                e[j][i] = 1e9;
            }
        }
    }

    for (int k = 1; k <= m; ++k) {
        int t1, t2, t3;
        cin >> t1 >> t2 >> t3;
        e[t1][t2] = t3;
    }

    for (int l = 1; l <= n; ++l) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                e[i][j] = min(e[i][j], e[i][l] + e[l][j]);
            }
        }
    }

    cout << e[1][4] << endl;
    return 0;
}

/*
4 8
1 2 2
1 3 6
1 4 4
2 3 3
3 1 7
3 4 1
4 1 5
4 3 12
*/